package com.example.bailin.giftsay.sort.activity;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseActivity;
import com.example.bailin.giftsay.entity.LmSkipEntity;
import com.example.bailin.giftsay.sort.adapter.LmTwoLvAdapter;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

/**
 * Created by bailin on 16/8/26.
 * <p>
 * 栏目的二级页面
 */
public class LmSkipActivity extends BaseActivity implements View.OnClickListener, AbsListView.OnScrollListener {

    private ListView lv;
    private TextView tvTitle; //  渐变的标题
    private RelativeLayout rlTitle; // 渐变的标题栏背景
    private ImageView ivReturn;
    private ImageView ivLanmuTwostage;
    private String title;

    @Override
    protected int setLayout() {
        return R.layout.fragment_selection;
    }

    @Override
    protected void initView() {
        lv = bindView(R.id.lv_selection);
        tvTitle = bindView(R.id.tv_item_selection_title);
        ivReturn = bindView(R.id.iv_selection_return);
        rlTitle = bindView(R.id.rl_selection_title);
        ivReturn.setOnClickListener(this);
        lv.setOnScrollListener(this);


    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        int id = intent.getIntExtra("id", 0);
        tool().getData(UrlProvide.LM_SKIP + id + UrlProvide.LM_SKIP1, LmSkipEntity.class, new NetTool.NetInterface<LmSkipEntity>() {
            @Override
            public void onSuccess(LmSkipEntity lmSkipEntity) {
                View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_lm_head, null);
                TextView tvDescription = (TextView) view.findViewById(R.id.tv_lm_description);
                TextView tvImgTitle = (TextView) view.findViewById(R.id.tv_img_title);
                tvImgTitle.setText(title);
                ivLanmuTwostage = (ImageView) view.findViewById(R.id.iv_lanmu_twostage);
                tvDescription.setText(lmSkipEntity.getData().getDescription());
                tool().getImg(lmSkipEntity.getData().getCover_image_url(), ivLanmuTwostage);
                LmTwoLvAdapter adapter = new LmTwoLvAdapter();
                adapter.setLmSkipEntity(lmSkipEntity);
                lv.setAdapter(adapter);
                lv.addHeaderView(view);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_selection_return:
                finish();
                break;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (view.getChildAt(0) != null) {
            getScrolly();
            if (getScrolly() <= 0) {
                tvTitle.setText(null);
                rlTitle.setBackgroundColor(Color.argb(0, 244, 62, 94));//AGB由相关工具获得，或者美工提供
            } else if (getScrolly() > 0 && getScrolly() <= 200) {
                float scale = (float) getScrolly() / 200;
                float alpha = (255 * scale);
                rlTitle.setBackgroundColor(Color.argb((int) alpha, 244, 62, 94));
            } else {
                rlTitle.setBackgroundColor(Color.argb(255, 244, 62, 94));
                float scale = (float) getScrolly() / 200;
                float alpha = (255 * scale);

                if (getScrolly() > 200 && getScrolly() <= 400) {
                    tvTitle.setText(title);
                    tvTitle.setTextColor(Color.argb((int) alpha, 255, 255, 255));
                }
                if (getScrolly() > 400) {
                    tvTitle.setText(title);
                    tvTitle.setTextColor(Color.argb(255, 255, 255, 255));
                }

            }
        }
    }

    public int getScrolly() {
        View view = lv.getChildAt(0);
        if (view == null) {
            return 0;
        }
        int firstVisiblePosition = lv.getFirstVisiblePosition();
        int top = view.getTop();
        return -top + firstVisiblePosition * ivLanmuTwostage.getHeight();
    }
}